#!/usr/bin/perl -w
use strict;
use CGI;
use LWP::UserAgent;

my $q = CGI->new;
$q->charset('utf-8');
if ($q->param('src')) {
  print $q->header('text/plain');
  open my $fh, $0 or die $!;
  print while <$fh>;
  close $fh;
  exit;
}
my $url = $q->param('url');
unless ($url) {
  print $q->header('text/plain','403 Forbidden'), '403 Forbidden';
  exit;
}
my $ua = LWP::UserAgent->new;
$ua->agent("aproxy/0.01");
$ua->timeout(10);
my $r = $ua->get($url);
unless ($r->is_success) {
  print $q->header('text/plain','404 Not Found'), '404 Not Found';
  exit;
}
if ($r->content !~ m!<kml xmlns="http://earth.google.com/kml/2.0">!) {
  print $q->header('text/plain','403 Forbidden'), '403 Forbidden';
  exit;
}
print $q->header('text/xml'), $r->content;
